<template>
  <div class="login">
    <hmNanBar path="/home/find"></hmNanBar>
    <div class="content">
      <h3 class="tlite">您好，请登录</h3>
      <van-form ref="form">
        <van-field
          placeholder="请输入手机号"
          v-model="form.mobile"
          :rules="rules.mobile"
          name="mobile"
        >
          <template #left-icon>
            <i class="iconfont" style="margin-right:10px">&#xe60b;</i>
          </template>
        </van-field>
        <van-field
          placeholder="请输入验证码"
          v-model="form.code"
          :rulse="rules.code"
        >
          <template #left-icon>
            <i class="iconfont" style="margin-right:10px">&#xe60c;</i>
          </template>
          <template #button>
            <!-- 对获取验证码进行一个判断 满足定时器条件的话就显示获取验证码 否则显示倒计时-->
            <span class="code" @click="getCode" v-if="totalTime === 60"
              >获取验证码</span
            >
            <span class="code" v-else>{{ totalTime }}s后重试</span>
          </template>
        </van-field>
      </van-form>
      <div class="thore">
        登录即同意<span>《用户使用协议》</span> 和 <span>《隐私协议》</span>
      </div>
      <van-button type="primary" block round color="red" @click="submit"
        >确认</van-button
      >
    </div>
  </div>
</template>
<script>
import { auCode, auLogin } from '@/api/login.js'
// 导入token的功能
import { setLocal, getLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      // 定义一个定时器
      totalTime: 60,
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请输入正确手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  created () {
    if (getLocal('token')) {
      this.$router.push('/home')
    }
  },
  methods: {
    // 验证码验证
    getCode () {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // 插入一个定时器方法//从点击后就开始倒计时 使后面的倒计时不可再点击
          this.totalTime--
          // 定义一个计数器
          const _interval = setInterval(() => {
            this.totalTime--
            // 点击后就开始计时// 当计数器小于等于0的时候就清除定时器（clearInterval）并且恢复定时器  为下一次点击做准备
            if (this.totalTime <= 0) {
              clearInterval(_interval)
              this.totalTime = 60
            }
          }, 1000)
          // 发送验证码的axios toast这个是在vant中是提示小黑窗显示的意思
          auCode({ mobile: this.form.mobile }).then(res => {
            this.$toast.success(res.data.data)
            this.form.code = res.data.data
          })
        })
        .catch(() => {
          // 表示 验证失败
          this.$toast.fail('验证失败')
        })
    },
    // 点击全局表单验证
    submit () {
      this.$refs.form
        .validate()
        .then(() => {
          auLogin(this.form)
            .then(res => {
              window.console.log(res)
              // 保存token
              setLocal('token', res.data.data.jwt)
              // 保存用户信息
              this.$store.commit('setUserInfo', res.data.data.user)
              this.$store.commit('setIsLogin', true)
              const _redirect = this.$route.query.redirect
              if (_redirect) {
                this.$router.push(_redirect)
              } else {
                this.$router.push('/home')
              }
            })
            .catch(err => {
              window.console.log(err)
            })
        })
        .catch(() => {
          // window.console.log(err)
        })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  .content {
    padding: 50px @p15 0;
    .tlite {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
      margin-bottom: 32px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 22px;
      letter-spacing: 0px;
    }
    .thore {
      height: 16px;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16px;
      letter-spacing: 0px;
      margin-top: 15px;
      margin-bottom: 44px;
      span {
        color: #00b8d4;
      }
    }
  }
}
</style>
